package com.br.extreme.smartstock.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;

import com.google.gson.Gson;

public class GsonUtil {
	public static <T> T getJsonResult(HttpResponse response, Class<T> clazz) throws Exception{
		BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		if(response.getStatusLine().getStatusCode() != 200){
			String line = "";
			if((line = reader.readLine())!= null){
				System.out.println(line);
			}
			
		};		
		
		T result = (T) new Gson().fromJson(reader, clazz);
		reader.close();
		return result;
		
	}
	
	public static <T> String toJson(T entity){
		return new Gson().toJson(entity);
	}
}
